import threading
import time

import airsim
from airsim import Pose, Vector3r
class UavFly(threading.Thread):
    def __init__(self,vehicle_name,sleep_time):
        threading.Thread.__init__(self)
        self.vehicle_name = vehicle_name
        self.sleep_time = sleep_time
    def run(self):
        time.sleep(self.sleep_time*12)
        client = airsim.MultirotorClient()
        client.confirmConnection()

        if self.vehicle_name != "000":
            pose = Pose(Vector3r(0, 0, 0))
            client.simAddVehicle(self.vehicle_name, "simpleflight", pose)
        client.enableApiControl(True,vehicle_name=self.vehicle_name)
        client.armDisarm(True, vehicle_name=self.vehicle_name)
        client.takeoffAsync(vehicle_name=self.vehicle_name).join()
        client.moveToPositionAsync(0, 0, -10, 1, vehicle_name=self.vehicle_name).join()
        client.moveToPositionAsync(-30-self.sleep_time*2, 0, -10, 1, vehicle_name=self.vehicle_name).join()
        client.landAsync(vehicle_name=self.vehicle_name).join()

three=[[
    [0,2,-8],
    [0,-2,-8],
    [0,-2,-12],
    [0,2,-12],
    [0,2,-8]
],
    [
        [-30, 2, -8],
        [-30, -2, -8],
        [-30, -2, -12],
        [-30, 2, -12],
        [-30, 2, -8]
    ],
    [
[0,2,-8],
[-30, 2, -8]
    ],
    [[0,-2,-8],
[-30, -2, -8]
     ],
    [
[0,-2,-12],
[-30, -2, -12]
    ],
    [
[0,2,-12],
[-30, 2, -12],
    ]
]
client = airsim.MultirotorClient()
try:

    for init in three:
        vector_points = [Vector3r(x, y, z) for x, y, z in init]
        color_rgba = [0.0, 1.0, 0.0, 1.0]
        client.simPlotLineStrip(
            points=vector_points,
            color_rgba=color_rgba,
            thickness=8,
            duration=3000
        )
    print("线条绘制成功！")
except Exception as e:
    print(f"错误：{e}")
time.sleep(30)
vehicle_names=["000","001","002","003"]
i=0
for vehicle_name in vehicle_names:
    process=UavFly(vehicle_name=vehicle_name,sleep_time=i)
    process.start()
    i+=1
